var faceoz = require('ui/faceoz');
var fuel_query_config = require('ui/common/fuel_query/config');
var _current_page_name = 'fuel_query_result_list';
var _table_view = null;
var _data = [];
var _win = null;
var _city_id = 0;
var _fuel_type_id = 0;
var _containing_tab = null;

/*
 *  select city
 */
function _init_controls() {
	try {
		_table_view = Ti.UI.createTableView({
			data : _data,
			style : Ti.UI.iPhone.TableViewStyle.PLAIN,
			minRowHeight : faceoz.default_table_view_row_height
		});
		_win.add(_table_view);
	} catch(err) {
		faceoz.process_simple_error_message(err, _current_page_name + ':_init_controls');
		return;
	}
}

/*
 * select fuel type
 */
function _init_vars() {
	try {
		var db = Titanium.Database.open(faceoz.database_name);
		var rows = null;
		if (_fuel_type_id == -1) {
			rows = db.execute('select * from my_fuel where city_id=?', _city_id);
		} else {
			rows = db.execute('select * from my_fuel where city_id=? and fuel_type_id=?', _city_id, _fuel_type_id);
		}
		var i = 0;
		while (rows.isValidRow()) {
			var row = Ti.UI.createTableViewRow({
				className : 'class_name_' + i,
				hasCheck : false
			});
			var fuelTypeNameLabel = Ti.UI.createLabel({
				text : fuel_query_config.get_fuel_name(_city_id, rows.fieldByName('fuel_type_id')),
				top : 5,
				left : 10,
				width : 100,
				height : 20,
				textAlign : 'left',
				font : {
					fontSize : faceoz.normal_font_size,
					fontWeight : faceoz.font_weight
				}
			});
			row.add(fuelTypeNameLabel);

			var priceLabel = Ti.UI.createLabel({
				text : rows.fieldByName('price'),
				top : 5,
				right : 10,
				width : 200,
				height : 20,
				textAlign : 'right',
				font : {
					fontSize : faceoz.normal_font_size,
					fontWeight : faceoz.font_weight
				}
			});
			row.add(priceLabel);

			var trendURLImage = Ti.UI.createImageView({
				trend_url : rows.fieldByName('trend_url'),
				image : rows.fieldByName('trend_url'),
				top : 30,
				right : 10,
				left : 10,
				width : faceoz.screen_width - 20,
				height : 150
			});
			row.add(trendURLImage);
			trendURLImage.addEventListener('click', function(e) {
				var show_big_image_for_trend_win = require('ui/common/fuel_query/show_big_image_for_trend');
				show_big_image_for_trend_win = new show_big_image_for_trend_win({
					trend_url : e.source.trend_url,
					containing_tab : _containing_tab
				});
				_containing_tab.open(show_big_image_for_trend_win);
			});
			_data.push(row);
			i++;
			rows.next();

		}
		db.close();
	} catch(err) {
		faceoz.process_simple_error_message(err, _current_page_name + ':_init_vars');
		return;
	}
}

function fuel_query_result_list(args) {
	_data = [];
	_city_id = args.city_id;
	_fuel_type_id = args.fuel_type_id;
	_containing_tab = args.containing_tab;
	_win = Ti.UI.createWindow({
		title : fuel_query_config.get_city_name(_city_id),
		containing_tab : args.containing_tab,
		tabBarHidden : true
	});
	_init_vars();
	_init_controls();
	return _win;
}

module.exports = fuel_query_result_list;
